package com.fone.player.activity.local.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.entity.MediaFolder;
import com.fone.player.util.L;

public class LocalFolderAdapter extends BaseAdapter {

	private List<MediaFolder> mList;
	private Context mContext;
	private String TAG = "LocalFileAdapter";
	private ListView mListView;

	public LocalFolderAdapter(Context context, List<MediaFolder> list,
			ListView listview) {
		this.mContext = context;
		this.mList = list;
		this.mListView = listview;
	}

	@Override
	public int getCount() {
		int count = 0;
		if (mList != null) {
			count = mList.size();
		}
		return count;

	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);

	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.personal_local_file_item, null);
			viewHolder.iv_pic = (ImageView) convertView
					.findViewById(R.id.iv_file_pic);
			viewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_file_name);
			viewHolder.tv_num = (TextView) convertView
					.findViewById(R.id.tv_file_num);
			viewHolder.tv_position = (TextView) convertView
					.findViewById(R.id.tv_file_position);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		MediaFolder bean = mList.get(position);
		viewHolder.iv_pic.setTag(position);

		viewHolder.tv_name.setText(bean.getMediaFolderName() + "");
		viewHolder.tv_num.setText("数量: " + bean.getMediaFolderTotalNum());
		viewHolder.tv_position.setText(bean.getMediaFolderPath());
		return convertView;

	}

	static class ViewHolder {
		public ImageView iv_pic;
		public TextView tv_name;
		public TextView tv_num;
		public TextView tv_position;
	}

	public void refreshMediaView(int position, Bitmap bitmap) {
		ImageView imageViewByTag = (ImageView) mListView
				.findViewWithTag(position);
		L.v(TAG, "refreshMediaView2", "position:" + position
				+ ",imageViewByTag:" + imageViewByTag);
		if (imageViewByTag != null) {
			if (bitmap != null) {
				imageViewByTag.setImageBitmap(bitmap);
			} else {
				imageViewByTag.setImageResource(R.drawable.local_folder);
			}

		}
	}
}
